package com.ly.mp.dal.comm.config.condition;

import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.PropertyResolver;
import org.springframework.core.type.AnnotatedTypeMetadata;


public abstract class TransactionPolicy implements Condition {
    public static final String TRANSACTIONPOLICY_NONE = "none";
    public static final String TRANSACTIONPOLICY_NORMAL = "normal";
    public static final String TRANSACTIONPOLICY_JTA = "jta";
    public static final String TRANSACTIONPOLICY_TCC = "tcc";
    public static final String TRANSACTIONPOLICY_SHARD = "shard";

    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        String transactionPolicy = this.getTransactionPolicy(context);
        return StringUtils.isNotBlank(transactionPolicy) && transactionPolicy.equals(this.getTransactionPolicy());
    }

    protected abstract String getTransactionPolicy();

    protected String getTransactionPolicy(ConditionContext context) {
        PropertyResolver resolver = context.getEnvironment();
        return resolver.getProperty("write.mp.jdbc.transactionPolicy");
    }
}